package com.cskaoyan.config;

import com.aliyun.oss.OSSClient;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/**
 * @author hsp
 * @date 2021/5/11 9:24
 */
@Component
@Data
@ConfigurationProperties(prefix = "aliyun")
public class AliyunCompoent {
    String accessKeyId;
    String accessKeySecret;
    Oss oss;
    Sms sms;
    public OSSClient getOssClient(){
        OSSClient ossClient = new OSSClient(oss.getEndPoint(), accessKeyId, accessKeySecret);
        return  ossClient;
    }

    public void  FileUpload(String fileName,InputStream inputStream){
        OSSClient ossClient = getOssClient();
        //String key -->fileName
        ossClient.putObject(oss.getBucket(),fileName,inputStream);
    }

    public void sendMsg(String mobilePhone, String code) {
        String signName = sms.getSignName();
        String templateCode = sms.getTemplateCode();

        String accessKeyIdTeacher = "LTAI5tSHBBV5把我加上再传到云端b1PkUocrkr4M";
        String accessKeySecretTeacher = "shGJrj1mxSK0jcg把我加上再传到云端Gy5WDtgTMabyvo4";
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyIdTeacher, accessKeySecretTeacher);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        request.putQueryParameter("PhoneNumbers", mobilePhone);


        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println(response.getData());


    }
}
